//
// Created by moon on 2023/12/5.
//
#include "usart.h"
#include "ArduinoAPI/Arduino.h"

#include "ArduinoAPI/Wire.h"

//OLed oled;

/**
 *	stm32f4调试时外部始终无法初始化，需要先使用内部时钟来配置外部时钟，
 *
 *	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
	{
		Error_Handler();
	}
 *	......
 *	外部时钟配置
 */
extern "C" void Main() {
    // USER_LOG("system clock: %lu", HAL_RCC_GetSysClockFreq());

    Serial.begin(115200);
    String str = "system init\n";
    Serial.print(str);

    Serial.printf("hello world: %s", __DATE__);

    uint8_t  buff[64] = {0};

	Wire.readByte(0x68, 0x75, &buff[1]);
	Wire.readByte(0x68, 0x6b, &buff[0]);

	Wire.writeByte(0x68, 0x6b, 0x2);
	Wire.readByte(0x68, 0x6b, &buff[0]);

    for (;;) {

    	HAL_I2C_Mem_Read(&hi2c1, (0x68 << 1) | 1, 0x75, I2C_MEMADD_SIZE_8BIT, buff, 2, 50);

        // 接收缓冲区数据可用
        if (Serial.available()) {

            auto ret = Serial.read(buff, 64);
            Serial.write(buff, ret);

//            HAL_GPIO_TogglePin(UserLed_GPIO_Port, UserLed_Pin);
        }
//        delay_us(20);

		delay(1000);

    }
}

